# pylint: disable-msg=E1101
"""
@author: Sergiy Tsymbalyuk
"""

from datetime import datetime
from chowa.lib.base import Session
from chowa.model.site import Message, Receipt
from chowa.model.auth import User

def send_message(text, sender_id=None, recipient_id=None):
    msg = Message(text=text, sender_id=sender_id, created=datetime.now())
    Session.add(msg)
    Session.commit()
    rcpt = Receipt(message_id=msg.id, recipient_id=recipient_id)
    Session.add(rcpt)
    Session.commit()

def send_system_message(text, recipient_id=None):
    system = Session.query(User).filter_by(username='system').one()
    send_message(text, system.id, recipient_id)
